package com.samphanie.auiu.auth.security.service;

import com.samphanie.auiu.auth.domain.SecurityUser;
import com.samphanie.auiu.auth.service.AuiuUserDetailsService;
import com.samphanie.auiu.auth.service.IUserService;
import com.samphanie.auiu.mbg.model.User;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author Dries
 * @email 1451691457@qq.com
 */
@Service
@RequiredArgsConstructor
public class UserDetailServiceImpl implements AuiuUserDetailsService {

    private final IUserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.findOptionalByUsername(username).orElseThrow(() -> new UsernameNotFoundException("Cannot find username"));
        return SecurityUser.fromSecurityUser.apply(user);
    }

    /**
     * 根据手机号登录
     *
     * @param mobile
     * @return UserDetails
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByMobile(String mobile) throws UsernameNotFoundException {
        User user = userService.findOptionalByMobile(mobile).orElseThrow(() -> new UsernameNotFoundException("Cannot find username"));
        return SecurityUser.fromSecurityUser.apply(user);
    }

    /**
     * 根据社交账号登录
     *
     * @param openId 第三方的绑定的openId
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserBySocial(String openId) throws UsernameNotFoundException {
        return null;
    }
}
